/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : my_score_rd.js                            
*  Description  : 스코어 상세
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-07-12  김효순    Created.  
*******************************************************************************/

jQuery(function($){
	// 라운드정보
	function clsRoundInfo()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsRoundInfo.prototype = {
		_init: function(){
			var $layer = $("div#container div.my_round");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("tbody tr:eq(2) p.img img").width(130).height(90);
			
			this.setInfo({
				"날짜": ""
				, "요일": ""
				, "골프장코스명": ""
				, "홀": ""
				, "파일정보": {
					"파일명": ""
				}
				, "내용": ""
			});
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this.$getLayer();
			var d = this.infoData;
			
			$layer.find("tbody tr:eq(0) span.day").html(d["날짜"] + "(" + d["요일"] + ")");
			$layer.find("tbody tr:eq(0) span.club_name").html(d["골프장코스명"]);
			$layer.find("tbody tr:eq(0) span:eq(2)").html("");
			$layer.find("tbody tr:eq(0) span:eq(3)").html(d["홀"]);
			
			$layer.find("tbody tr:eq(2) p.img img").attr("src", _gf.filePath2Url(d["파일정보"]["파일명"])).error(function(){
				$(this).attr("src", _ROOT + "/images/board/default_130_90.gif");
			});
			$layer.find("tbody tr:eq(2) p.txt").html(d["내용"]);
		}
		, spinStart: function(){
			var $layer = this.$getLayer();
			
			$layer.find("table").hide();
			if($layer.find("div#tmpSpin").length == 0){
				$layer.append("<div id='tmpSpin' style='position:relative; width:900px; height:150px;' />");
			}
			$layer.find("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this.$getLayer();
			
			$layer.find("table").show();
			$layer.find("div#tmpSpin").hide().spin(false);
		}
	}
	
	// 홀별기록
	function clsHoleRecord()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsHoleRecord.prototype = {
		_init: function(){
			var $layer = $("div#container article.my_hole_list01");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("tbody tr:eq(2) th:eq(0)").html("스코어");
			
			this.setList({
				"파": []
				, "스코어": []
			});
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this.$getLayer();
//			var m = this.listData["파"].length/2;
			var thisClass = this;
//			
//			$layer.find("thead tr th:gt(0)").filter(function(){
//				return !$(this).attr("class");
//			}).remove();
//			$layer.find("tbody td").filter(function(){
//				return !$(this).attr("class");
//			}).remove();
//			
//			for(var i=0;i<this.listData["파"].length;i++){
//				if(i < m){
//					$layer.find("thead tr th:eq(0)").after("<th />");
//					$layer.find("tbody tr th:nth-child(1)").after("<td />");
//				}else{
//					$layer.find("thead tr th.tb_c:eq(0)").after("<th />");
//					$layer.find("tbody tr").each(function(){
//						$(this).children("td.tb_c:eq(0)").after("<td />");
//					});
//				}
//			}
//			
//			$layer.find("thead tr th:gt(0)").filter(function(){
//				return !$(this).attr("class");
//			}).each(function(n){
//				$(this).html(n+1);
//			});
			
			$layer.find("tbody tr:eq(0) td").filter(function(){
				return !$(this).attr("class");
			}).each(function(n){
				$(this).html(thisClass.listData["파"][n]);
			});
			
			$layer.find("tbody tr:eq(1) td").filter(function(){
				return !$(this).attr("class");
			}).each(function(n){
				$(this).html(thisClass.listData["스코어"][n] - thisClass.listData["파"][n]);
			});
			
			$layer.find("tbody tr:eq(2) td").filter(function(){
				return !$(this).attr("class");
			}).each(function(n){
				$(this).html(thisClass.listData["스코어"][n]);
			});
			
			this.calc();
		}
		, calc: function(){
			var $layer = this.$getLayer();
			var m = this.listData["파"].length/2;
			
			var pSum = {
				"소계0": 0
				, "소계1": 0
			}
			
			var sSum = {
				"소계0": 0
				, "소계1": 0
			}
			
			for(var i=0;i<this.listData["파"].length;i++){
				if(i < m){
					pSum["소계0"] += this.listData["파"][i];
					sSum["소계0"] += this.listData["스코어"][i];
				}else{
					pSum["소계1"] += this.listData["파"][i];
					sSum["소계1"] += this.listData["스코어"][i];
				}
			}
			
			$layer.find("tbody tr:eq(0) td").eq(m).html(pSum["소계0"]);
			$layer.find("tbody tr:eq(0) td").eq(this.listData["파"].length+1).html(pSum["소계1"]);
			$layer.find("tbody tr:eq(0) td:last").html(pSum["소계0"] + pSum["소계1"]);
			
			$layer.find("tbody tr:eq(1) td").eq(m).html(sSum["소계0"] - pSum["소계0"]);
			$layer.find("tbody tr:eq(1) td").eq(this.listData["파"].length+1).html(sSum["소계1"] - pSum["소계1"]);
			$layer.find("tbody tr:eq(1) td:last").html(sSum["소계0"] - pSum["소계0"] + sSum["소계1"] - pSum["소계1"]);
			
			$layer.find("tbody tr:eq(2) td").eq(m).html(sSum["소계0"]);
			$layer.find("tbody tr:eq(2) td").eq(this.listData["파"].length+1).html(sSum["소계1"]);
			$layer.find("tbody tr:eq(2) td:last").html(sSum["소계0"] + sSum["소계1"]);
		}
	}
	
	// 스코어 카드요약
	function clsScoreCardStat()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsScoreCardStat.prototype = {
		_init: function(){
			var $layer = $("div#container article.my_hole_list02");
			this._HTMLDom = {
				"$layer": $layer
			}		
			this.setList({
				"파": []
				, "타": []
			});
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var thisClass = this;
			
			var sum = {
				"4이상": 0
				, "트리플보기": 0
				, "더블보기": 0
				, "보기": 0
				, "파": 0
				, "버디": 0
				, "이글": 0
				, "홀인원": 0
			}
			
			var aMap = [
				"4이상"
				, "트리플보기"
				, "더블보기"
				, "보기"
				, "파"
				, "버디"
				, "이글"
				, "홀인원"
			];
			
			for(var i=0;i<this.listData["파"].length;i++){
				var p = parseInt(this.listData["파"][i]);
				var t = parseInt(this.listData["타"][i]);
				
				p = p || 0;
				t = t || 0;
				
				if(p < 3 || p > 5) continue;
				
				if(t >= 0){
					switch(t){
						case 0:
							sum["파"]++;
							break;
						case 1:
							sum["보기"]++;
							break;
						case 2:
							sum["더블보기"]++;
							break;
						case 3:
							sum["트리플보기"]++;
							break;
						default:
							sum["4이상"]++;
							break;
					}
				}else if(t == -1){
					sum["버디"]++;
				}else if((p + t) == 1){
					sum["홀인원"]++;
				}else if((p == 4 || p == 5) && t == -2){
					sum["이글"]++;
				}
			}
			
			$layer.find("tbody td").each(function(n){
				$(this).html(sum[aMap[n]]);
			});
		}
	}
	
	
	// 서버통신
	function clsComm()
	{
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			
		}
		// 상세정보
		, getScoreCardInfo: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "getMyScoreCard";
			var options = {
				"reservSeq": arg["스코어카드코드"]
			}
			_gf.ajax({
				"sid": "US001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": {}
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						var golfCourseName = "";
						if(arg["스코어카드코드"].substrEx(0, 1) == "D"){
							golfCourseName = _util.nvl(obj["golfclubId"]);
						}else{
							golfCourseName = _util.nvl(obj["golfclubName"]) + "/" + _util.nvl(obj["courseInfoName"]);
						}
						
						if(i==0){
							res["결과값"] = {
								"날짜": _util.nvl(obj["reservDate"])
								, "요일": _util.nvl(obj["week"])
								, "골프장코스명": golfCourseName
								, "홀": 0
								, "파일정보": {
									"파일코드": _util.nvl(obj["fileSeq"])
									, "파일명": _util.nvl(obj["path"]) + _util.nvl(obj["saveFileName"])
									, "원본파일명": _util.nvl(obj["orgnFileName"])
								}
								, "내용": _util.nvl(obj["contents"])
								, "파": []
								, "스코어": []
							}
						}
						
						res["결과값"]["홀"] += _util.nvl(obj["holeCnt" + (i+1)], 0);
						
						for(j=0;j<_util.nvl(obj["holeCnt" + (i+1)], 0);j++){
							res["결과값"]["파"].push(_util.nvl(obj["par" + (j+1) + "h"], 0));
							res["결과값"]["스코어"].push(_util.nvl(obj["score" + (j+1) + "h"], 0));
						}
					}
				}
				
				document.objControl.finishScoreCardInfo(res);
			}, true);
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objRoundInfo = null;
		this.objHoleRecord = null;
		this.objScoreCardStat = null;
		
		this.scoreCardKey = null;
		
		this._permitStart = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objRoundInfo = new clsRoundInfo();
			this.objHoleRecord = new clsHoleRecord();
			this.objScoreCardStat = new clsScoreCardStat();
			
			this._beforeStart();
			this._bind();
		}
		, _preload: function(){
			_gf.spinStart();
			
			this.scoreCardKey = _util.getParam("q_seq");
		}
		, _beforeStart: function(){
			var thisClass = this;
			this._permitStart = {
				
			}
			
			$.ajax({
				url: _ROOT + "/js/mypage_common.js"
				, dataType: "script"
				, async: false
				, success: function(){
					
				}
				, error: function(){
					alert("error");
				}
			});
			
			_util.executeThread(function(){
				for(var k in thisClass._beforeStart){
					if(!thisClass._beforeStart[k]) return false;
				}
				
				return (_gf.getMyInfoData());
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _bind: function(){
			var thisClass = this;
			
			// 나의 스코어, 스코어 등록
			$("div#container").on("click", "img[alt='나의 스코어'], img[alt='스코어 등록'], img[alt='목록'], img[alt='수정']", function(){
				var bName = $(this).attr("alt");
				
				switch(bName){
					case "나의 스코어":
						thisClass.goScoreListPage();
						break;
					case "스코어 등록":
						thisClass.goScoreWritePage();
						break;
					case "목록":
						thisClass.goScoreListPage();
						break;
					case "수정":
						thisClass.goScoreWritePage(thisClass.scoreCardKey);
						break;
				}
			});
		}
		, _start: function(){
			// 상세정보
			this.objRoundInfo.spinStart();
			this.objComm.getScoreCardInfo({
				"스코어카드코드": this.scoreCardKey
			});
		}
		// 상세 정보 완료
		, finishScoreCardInfo: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objRoundInfo.spinEnd();
			
			this.objRoundInfo.setInfo(ret["결과값"]);
			this.objHoleRecord.setList(ret["결과값"]);
			this.objScoreCardStat.setList({
				"파": ret["결과값"]["파"]
				, "타": ret["결과값"]["스코어"] - ret["결과값"]["파"]
			});
		}
		// 나의 스코어
		, goScoreListPage: function(){
			var szLink = _ROOT + "/mypage/my_score.html";
			document.location.href = szLink;
		}
		// 스코어 등록
		, goScoreWritePage: function(seq){
			seq = seq || "";
			var szLink = _ROOT + "/mypage/my_score_write.html?q_seq=" + seq;
			
			document.location.href = szLink;
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});
